package com.lechi.web.service.impl;

import com.lechi.web.common.annotation.DataScope;
import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotDeviceGroupService;
import com.lechi.web.system.domain.IotDeviceGroupBo;
import com.lechi.web.system.mapper.iot.IotDeviceGroupMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 设备组Service业务层处理
 *
 * @author yangguang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotDeviceGroupServiceImpl implements IIotDeviceGroupService {

    private final IotDeviceGroupMapper iotDeviceGroupMapper;

    /**
     * 查询设备组
     *
     * @param id 设备组主键
     * @return 设备组
     */
    @Override
    public IotDeviceGroupBo selectIotDeviceGroupById(Long id) {
        try {
            return iotDeviceGroupMapper.selectIotDeviceGroupById(id);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过id查询设备组", method, id));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询设备组列表
     *
     * @param iotDeviceGroupBo 设备组
     * @return 设备组
     */
    @Override
    @DataScope(deviceAlias = "d")
    public List<IotDeviceGroupBo> selectIotDeviceGroupList(IotDeviceGroupBo iotDeviceGroupBo) {
        try {
            List<IotDeviceGroupBo> iotDeviceGroupBos = null;
            if ("1".equals(iotDeviceGroupBo.getNote())) {
                iotDeviceGroupBos = iotDeviceGroupMapper.selectIotDeviceGroupList(iotDeviceGroupBo);
            } else {
                iotDeviceGroupBos = iotDeviceGroupMapper.selectAllDeviceGroupList(iotDeviceGroupBo);
            }
            return iotDeviceGroupBos;
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询设备组列表", method, iotDeviceGroupBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增设备组
     *
     * @param iotDeviceGroupBo 设备组
     */
    @Override
    public void insertIotDeviceGroup(IotDeviceGroupBo iotDeviceGroupBo) {
        iotDeviceGroupBo.setCreateTime(new Date());
        try {
            iotDeviceGroupMapper.insertIotDeviceGroup(iotDeviceGroupBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增设备组", method, iotDeviceGroupBo));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改设备组
     *
     * @param iotDeviceGroupBo 设备组
     */
    @Override
    public void updateIotDeviceGroup(IotDeviceGroupBo iotDeviceGroupBo) {
        try {
            iotDeviceGroupMapper.updateIotDeviceGroup(iotDeviceGroupBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改设备组", method, iotDeviceGroupBo));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 批量删除设备组
     *
     * @param ids 需要删除的设备组主键
     */
    @Override
    public void deleteIotDeviceGroupByIds(Long[] ids) {
        try {
            iotDeviceGroupMapper.deleteIotDeviceGroupByIds(ids);
        } catch (Exception e) {
            log.error("Exception : {} ", e.getMessage());
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    /**
     * 删除设备组信息
     *
     * @param id 设备组主键
     */
    @Override
    public void deleteIotDeviceGroupById(Long id) {
        try {
            iotDeviceGroupMapper.deleteIotDeviceGroupById(id);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改设备组", method, id));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }
}
